home *** CD-ROM | disk | FTP | other *** search
- #Name: PlugIn Demo
- #Description: Nonsense demo that just uses and explains every OLE method (\optiperl\plug-ins\PlugInDemo.pl)
- #Icon: %opti%Tools.icl,146
- #Extensions: NewProcess
-
- #Note the 3 comments above. These are *absolutely* necessary for each
- #plug in. Also since we are using TK, the 4th line is also necessary.
- #These lines are parsed when OptiPerl loads or by selecting menu item
- #Tools / Plug-ins / Update plugins.
-
- use Tk;
- use Win32::OLE;
-
- #Now we will write our code. Notice that *all* our code is enclosed in subroutines.
- #Do not add code outside subroutines.
-
- #If you are using TK, this also includes *not* calling the
- #MainLoop;
- #sub anywhere. OptiPerl wfill take care of calling TK's message handler.
-
-
- sub Initialization {
- #The Initialization subroutine is called when the plug-in starts.
- #usage: Initialization(int PlugID)
-
- $plug_id = $_[0];
- #The first parameter is a handle to the plug-in. We will need this
- #for future commands
-
- $optiperl = Win32::OLE->new('OptiPerl.Application');
- #Initialize OLE object
-
- $main = MainWindow->new;
- #Initialize TK toplevel window
-
- $main->Label(-text => 'OptiPerl Plug-in Demo')->pack;
- #Add a label
-
- $log = $main->Listbox()->pack;
- AddLog("Initialization");
-
- $main->OnDestroy(sub{Finalization()});
- #IMPORTANT: Add an event handler when TK destroys itself to call
- #the Finalization subroutine.
-
- $win = $optiperl->RequestWindow($plug_id);
- #Request a dockable window in OptiPerl
-
- if (defined $win) {
- $win->Show;
- #Show the window
-
- $win->{Title} = "Plug-in Demo";
- #set the windows title
-
- ProcessEvents();
- #"Magic" subroutine in plugins to force TK to process
- #all windows events to create the window.
-
- $optiperl->DockWindow($plug_id, hex $main->winfo("id"), $win);
- #Dock TK's toplevel window into OptiPerl
-
- # The following code creates our menu system and toolbars
-
- # create a submenu.
- my $menu = $optiperl->CreateToolItem($plug_id,"menu");
- #set it's settings
- $menu->SetOptions("Tests","","","","");
-
- # create buttons. These will be items in the menu and also linked
- # in the toolbars.
- my $btn = $optiperl->CreateToolItem($plug_id,"button");
- # set the buttons options.
- # SetOptions(Caption, Hint, Image, Shortcut, On Click subroutine name)
- $btn->SetOptions(
- "Application Test","Test IApplication","%opti%tools.icl,117",
- "Alt+F5","IApplication_Test");
- # Note that setoptions is a fast way to set all the settings of
- # a tool item. We could also do:
- # $btn->{Enabled}=1;
- # $btn->{Hint}="test";
- # $btn->{Caption}="caption";
- # $btn->{Image}="cool.dll,100";
- # $btn->{Visible}=1;
- # $btn->{Shortcut}="Ctrl+Shift+F1";
- # $btn->{OnClick}="SubName";
- $menu->ToolLinks->Add($btn,0);
-
- $btn = $optiperl->CreateToolItem($plug_id,"button");
- $btn->SetOptions(
- "IDocument Test","Test IDocument","%opti%tools.icl,4",
- "Alt+F6","IDocument_Test");
- $menu->ToolLinks->Add($btn,0);
-
- $btn = $optiperl->CreateToolItem($plug_id,"button");
- $btn->SetOptions(
- "IProject Test","Test IProject","%opti%tools.icl,28",
- "Alt+F7","IProject_Test");
- $menu->ToolLinks->Add($btn,0);
-
- $btn = $optiperl->CreateToolItem($plug_id,"button");
- $btn->SetOptions(
- "ICodeExplorer Test","Test ICodeExplorer","%opti%tools.icl,100",
- "Alt+F8","ICodeExplorer_Test");
- $menu->ToolLinks->Add($btn,0);
-
- $btn = $optiperl->CreateToolItem($plug_id,"button");
- $btn->SetOptions(
- "Window Test","Test Window","%opti%tools.icl,137",
- "Alt+F9","Window_Test");
- $menu->ToolLinks->Add($btn,0);
-
- $btn = $optiperl->CreateToolItem($plug_id,"button");
- $btn->SetOptions(
- "Big Test","","%opti%tools.icl,134","Big Test","Big_Test");
- $menu->ToolLinks->Add($btn,1);
-
- $btn = $optiperl->CreateToolItem($plug_id,"button");
- $btn->SetOptions(
- "Exit","Close plug-in","%opti%tools.icl,1",
- "","Finalization");
- $menu->ToolLinks->Add($btn,1);
-
- $win->MainBarLinks->Add($menu,0);
- #add the menu to the menu of the window
-
- $win->PopUpLinks->AssignLinks($menu->ToolLinks);
- #add the menu to the pop-up menu (when the arrow is pressed)
-
- $optiperl->ToolBarLinks($plug_id)->AssignLinks($menu->ToolLinks);
- #add the menu to the main toolbar assigned to this plugin
- #(the one created next to the main menu)
-
- $optiperl->UpdateToolBars($plug_id);
- #Necessary! Error will occur if the above is not called after
- #messing around with the toolbars.
- $optiperl->ToolBarVisible($plug_id,1);
- $win->ReDraw;
- }
-
- # MessageBoxTest();
- }
-
-
- sub Finalization {
- #The Finalization sub is called when the plug-in terminates. If you press the
- #close button we created above, this will be called. However it will also be called
- #if the user closes manually this plug-in within optiperl.
-
- #Note: Don't pop-up any dialogs here asking questions like "do you want to
- #save changes" etc. These go in the OnCanTerminate subroutine below.
-
- $optiperl->CodeExplorer->DeleteNode($newnode);
-
- #Restore status bar
- $optiperl->StatusBarRestore;
- if (defined $win2) {
- $optiperl->DestroyWindow($plug_id,$win2);
- $win2->Hide;
- }
- $optiperl->DestroyWindow($plug_id,$win);
- $win->Hide;
- $optiperl->EndPlugIn($plug_id);
- }
-
- sub OnCanTerminate {
- AddLog("OnCanTerminate");
- #OnCanTerminate is called when the user wants to close OptiPerl.
- #If you use this subroutine, return true or false for whether optiperl
- #should be allowed to close.
-
- #For example here you could show a dialog with the text
- #"Would you like to save changes? " (with yes, no & cancel buttons).
- #If the user pressed YES, your scripts would save changes and
- #return 1; NO would ignore changes and return 1; CANCEL would
- #return 0.
-
- #Note that if you are going to return 1 to let optiperl exit, still don't
- #add any code that will uninitialize your script; because the user might
- #be running another plug-in that will not let optiperl terminate.
- #Any code to uninitialize your script goes into the Finalization subroutine.
-
- my $dialog = $main->Dialog(-text => 'Exit OptiPerl?',
- -bitmap => 'question',
- -title => 'Demo Plug-IN',
- -default_button => 'Yes',
- -buttons => [qw/Yes No/]);
-
- $win->Show;
- #Show the window, because the user might have minimized it
- my $winhandle = $optiperl->GetWindowHandle($win);
- #Get handle to the window that optiperl assigned to the plug-in
- $optiperl->GrabWindow($plug_id,1,$winhandle);
- #Disable input to optiperl, and tell it to keep this window
- #in the foreground
- my $answer = $dialog->Show;
- $optiperl->GrabWindow($plug_id,0,0);
- #Enable input in optiperl
- return ($answer eq 'Yes');
- }
-
- sub OnDocumentClose {
- AddLog("Closed: $_[0]");
- }
-
- sub OnBeforeAction {
- AddLog("Before: $_[0]");
- #Returning 0 will cancel executing the action.
- return 1;
- }
-
- sub OnAfterAction {
- AddLog("After: $_[0]");
- }
-
- sub OnParseStart {
- AddLog("OnParseStart");
- }
-
- sub OnParseEnd {
- AddLog("OnParseEnd");
- }
-
- # sub OnKeyEvent {
- # # A very low level key handler.
- # AddLog("Key $_[0] $_[1]");
- # return 1;
- # #returning 0 will cancel the key event!
- # }
-
- sub IApplication_Test {
-
- my $handle = $optiperl->Handle;
- #return a windows handle to optiperl application (if you need something like this)
-
- $optiperl -> OutputClear;
- #Clears the text in the "text" tab of the browser window
- $optiperl -> OutputAddLine("IApplication Test");
- #add a line in the "text" tab of the browser window
-
- $optiperl -> StatusBarText("Hello from this plug in");
- #set a string in the status bar
-
- $optiperl -> ExecuteAction("SelectLineAction");
- #Executes an action within optiperl. For a list of possible values, see the
- #file Options.ohk
-
- $BackgroundColor = $optiperl -> GetOpt("EditorColor");
- # The SetOpt & GetOpt methods controls optiperl's options
- $optiperl -> SetOpt("EditorColor",100000);
- $optiperl -> UpdateOptions(1);
- #Tell optiperl to update options that have to do with visible elements
- $optiperl -> SetOpt("EditorColor",$BackgroundColor);
- $optiperl -> UpdateOptions(1);
- }
-
-
- sub IDocument_Test {
- my $DocCount = $optiperl->DocumentCount;
- #Number of documents open in optiperl's editor
- my $doc0 = $optiperl->Documents(0);
- #Returns an IDocument with the first document open.
- my $linecount = $doc0->LineCount;
-
- $optiperl->NewDocument("New Test.pl");
- my $doc= $optiperl->ActiveDocument;
- #Returns an IDocument with the document currently being edited.
- $doc->Add("Test line 2");
- $doc->Insert(0,"Test line 1");
- $doc->Add("Test line 3");
- $doc->Delete(2);
- my $line = $doc->Lines(0);
- $doc->{CursorPosX}=0;
- $doc->{CursorPosY}=0;
- $doc->TempHightlightLine(0);
- }
-
- sub IProject_Test {
- my $project = $optiperl->Project;
- #Return an IProject with OptiPerl's project
- $project->NewProject('c:\perl pod files.prj');
- $project->AddFile('c:\perl\lib\pod\perl.pod');
- $project->AddFolder('c:\perl\lib\pod','*.pod');
- $project->SetOpt('LocalPath','c:\perl\lib\pod');
- $project->UpdateOptions;
-
- my $count = $project->Count;
- #Number of files in project
-
- AddLog($count);
-
- for (my $i = 0; $i < $count; $i++) {
- $item = $project->items($i);
- AddLog($item->Filename);
- }
-
- # note that if we add a new item with AddFile, it will not be necessarily at
- # $count number. Here is how to add or find an item and get it's options:
-
- $item = $project->AddFile('c:\perl\lib\pod\perl.pod');
- $item->{mode} = 755;
-
- $optiperl->ExecuteAction("ShowManagerAction");
- #show project manager window
- }
-
- sub ICodeExplorer_Test {
- my $treeview = $optiperl->CodeExplorer;
- #Return an ITreeview with OptiPerl's code explorer
- if (! defined $newnode) {
- $newnode = $treeview->
- AddNode($treeview->RootNode);
- $newnode->{Caption}="Test Node";
- for (my $i=0; $i<10; $i++)
- {
- my $node = $treeview->AddNode($newnode);
- $node->{Caption}="Node $i";
- }
- }
- }
-
- sub Window_Test {
- if (! defined $win2) {
- $top = $main->Toplevel;
- $top->Label(-text => 'Another Window')->pack;
- $win2 = $optiperl->RequestWindow($plug_id);
- $win2->Show;
- $win2->{Title} = "Plug-in Demo Second modal window";
- ProcessEvents();
- $optiperl->DockWindow($plug_id, hex $top->winfo("id"), $win2);
- }
- $win2->Show;
- }
-
- sub Big_Test {
- for (my $i=1; $i<=10; $i++)
- {
- ICodeExplorer_Test();
- IApplication_Test();
- IProject_Test();
- IDocument_Test();
- $optiperl->CodeExplorer->DeleteNode($newnode);
- $optiperl->CloseDocument;
- }
- }
-
- sub MessageBoxTest {
- my $string = $optiperl->InputBox('Plug in','Enter Text:','Default');
- $optiperl->OutputAddLine($string);
-
- my $MB_OK = 0;
- my $MB_OKCANCEL = 1;
- my $MB_ABORTRETRYIGNORE = 2;
- my $MB_YESNOCANCEL = 3;
- my $MB_YESNO = 4;
- my $MB_RETRYCANCEL = 5;
-
- my $MB_ICONHAND = 0x10;
- my $MB_ICONQUESTION = 0x20;
- my $MB_ICONEXCLAMATION = 0x30;
- my $MB_ICONASTERISK = 0x40;
-
- my $res=$optiperl->MessageBox("Plug-In","Message Box",$MB_OKCANCEL + $MB_ICONEXCLAMATION);
- $optiperl->OutputAddLine($res);
- }
-
-
- sub AddLog {
- $log -> insert("end", $_[0]);
- $log -> see("end");
- }
-
- # Debugging section.
- # A plug-in must not have any code in the main block.
- # However we can add some code here to run our subroutines
- # while developing our plug-in (so we can run it by pressing
- # "Run in console" in OptiPerl or debug it).
-
- # This can be done by checking if the magic variable
- # $valid_plugin is NOT defined.
-
- # There are however some limitations, for example
- # you cannot dock the window into optiperl.
-
- if (! defined $valid_plugin)
- {
- sub ProcessEvents {};
- Initialization;
- MainLoop;
- }